package com.itheima.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.IDishService;
import com.itheima.service.ISetmealService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private IDishService iDishService;

    @Autowired
    private ISetmealService iSetmealService;

    @PostMapping
    public R addCategory(@RequestBody Category category, HttpSession session) {
        if (category == null || StringUtils.isEmpty(category.getName()) || StringUtils.isEmpty(String.valueOf(category.getType())) || StringUtils.isEmpty(String.valueOf(category.getSort()))) {
            return R.error("请输入完整信息");
        } else {
            System.out.println(category);
            category.setCreateTime(LocalDateTime.now());
            category.setUpdateTime(LocalDateTime.now());
            category.setCreateUser((Long) session.getAttribute("LoginUser"));
            category.setUpdateUser((Long) session.getAttribute("LoginUser"));

            categoryService.save(category);
            return R.success("添加成功");
            //}


        }
    }

    @GetMapping("/page")
    public R pageQuery(Integer page, Integer pageSize) {
        Page<Category> objectPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        qw.orderByAsc(Category::getSort);
        categoryService.page(objectPage, qw);
        List<Category> records = objectPage.getRecords();
        long total = objectPage.getTotal();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return R.success(map);
        //return R.success(categoryService.page(new Page<>(page, pageSize)));


    }

    @DeleteMapping
    public R deleteById(Long id) {
//        LambdaQueryWrapper<Dish> dissQw = new LambdaQueryWrapper<>();
////      LambdaQueryWrapper<Dish> eq1 = qw.select("COUNT(*) FROM `dish` WHERE category_id ="+id);
//        categoryService.getById(qw);
//        LambdaQueryWrapper<Dish> eq = dissQw.eq(Dish::getCategoryId, id);
//        int count = iDishService.count(dissQw);
//
//
//        LambdaQueryWrapper<Setmeal> setMQw =new LambdaQueryWrapper<>();
//        LambdaQueryWrapper<Setmeal> eq2 = setMQw.eq(Setmeal::getCategoryId, id);
//
//        int count1 = iSetmealService.count(setMQw);
//
//        if (count>0||count1>0){
//            return  R.error("删除失败");
//        }else {
//            categoryService.removeById(id);
//            return R.success("删除成功");
//        }

        return ((iDishService.count(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, id)) > 0)
                || (iSetmealService.count(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, id)) > 0))
                ? R.error("删除失败")
                : R.success(categoryService.removeById(id));
//
//
//        return categoryService.removeById(id) ? R.success("删除成功") : R.error("删除失败");


    }

    @PutMapping
    public R updateByIdB(@RequestBody Category category) {


        return categoryService.updateById(category) ? R.success("删除成功") : R.error("删除失败");


    }

    @GetMapping("/list")
    public R queryDishList(Integer type) {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper();
        qw.eq(type!=null,Category::getType, type);
        // qw.s(Category::getType,type);
        List<Category> list = categoryService.list(qw);
        return R.success(list);
    }
}
